
#import "UILabel+X.h"

@implementation UILabel (X)


/**
 * 更具UILabel当前的字体及显示的文本，自动适配UILabel的高度
 * @result      返回UILabel调整高度过后的布局
 */
- (CGRect)heightToFit {
    CGSize size = CGSizeMake(self.frame.size.width, 4096);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
        NSDictionary *dicAttr = [NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, nil];
        NSStringDrawingOptions option = NSStringDrawingUsesFontLeading
        | NSStringDrawingTruncatesLastVisibleLine
        | NSStringDrawingUsesLineFragmentOrigin;
        
        size = [self.text  boundingRectWithSize:size
                                        options:option
                                     attributes:dicAttr
                                        context:nil].size;
    }
    else {
        size = [self.text sizeWithFont:self.font
                     constrainedToSize:size
                         lineBreakMode:self.lineBreakMode];
    }
    CGRect rc = self.frame;
    rc.size.height = ceilf(size.height);
    self.frame = rc;
    
    return rc;
}

@end
